// pages/group_forward/group_forward.js
var common = require("../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose: 1,
        money: 0,
        put_forward: "",
        service: 0,
        serviceType: 0,
        service_money: 0,
        payee_name: "",
        account1: "",
        account2: "",
        info: [],
        bankName: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getService();
        this.setData({
            money: options.money
        });
        this.readInfo();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    // onShareAppMessage: function () {
    // },
    choose: function(e) {
        var id = e.currentTarget.dataset.id;
        this.setData({
            choose: id
        });
    },
    putmoney: function(e) {
        var money = e.detail.value;
        var serviceType = this.data.serviceType;
        var service = this.data.service;
        var service_money = this.data.service_money;
        if (serviceType == 0) {
            service_money = service;
        } else {
            if (money == "") {
                money = 0;
            }
            service_money = parseFloat(money) * parseFloat(service / 100);
            if (money == 0) {
                money = "";
            }
        }
        this.setData({
            put_forward: money,
            service_money: service_money.toFixed(2)
        });
    },
    all: function(e) {
        var money = e.currentTarget.dataset.money;
        var serviceType = this.data.serviceType;
        var service = this.data.service;
        var service_money = this.data.service_money;
        if (serviceType == 0) {
            service_money = service;
        } else {
            service_money = parseFloat(money) * parseFloat(service / 100);
        }
        this.setData({
            put_forward: money,
            service_money: service_money.toFixed(2)
        });
    },
    getPayeeName: function(e) {
        this.setData({
            payee_name: e.detail.value
        });
    },
    getAccount1: function(e) {
        this.setData({
            account1: e.detail.value
        });
    },
    getAccount2: function(e) {
        this.setData({
            account2: e.detail.value
        });
    },
    getBankName(e) {
        this.setData({
            bankName: e.detail.value
        });
    },
    putForward: function(e) {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        var put_forward = that.data.put_forward;
        var payee_name = that.data.payee_name;
        var type = that.data.choose;
        var account = "";
        var all_money = that.data.money;
        let form_id = e.detail.formId;
        if (parseFloat(put_forward) > parseFloat(all_money)) {
            wx.showModal({
                title: "提示",
                content: "提现金额超出可提现佣金！"
            });
            return false;
        }
        if (type == 2) {
            account = that.data.account1;
        }
        if (type == 3) {
            account = that.data.account2;
        }
        if (payee_name == "" && type == 3) {
            wx.showModal({
                title: "提示",
                content: "请填写姓名！"
            });
            return false;
        }
        if (account == "" && type == 2) {
            wx.showModal({
                title: "提示",
                content: "请填写支付宝账号！"
            });
            return false;
        }
        if (account == "" && type == 3) {
            wx.showModal({
                title: "提示",
                content: "请填写银行卡号！"
            });
            return false;
        }
        common.request({
            endpoint: "Apply/financeapply/broker_submit",
            method: "post",
            data: {
                openid: openid,
                price: put_forward,
                account: account,
                payee_name: payee_name,
                type: type,
                form_id: form_id
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
                setTimeout(() => {
                    wx.redirectTo({
                        url: "/leaderPerson/pages/center/center"
                    });
                }, 1500);
            },
            fail: res => {
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
            }
        });
    },
    getService: function() {
        var that = this;
        common.request({
            endpoint: "Apply/financeapply/brokerage",
            method: "post",
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    service: res.extra.money_service,
                    serviceType: res.extra.type
                });
            },
            fail: res => {}
        });
    },
    readInfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Apply/financeapply/readinfo",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                var info = res.extra.trans_type;
                that.setData({
                    account1: res.extra.trans_type[1].account,
                    payee_name: res.extra.trans_type[2].payee_name,
                    account2: res.extra.trans_type[2].account,
                    info: res.extra.trans_type
                });
                for (var i = 0; i < info.length; i++) {
                    if (info[i].status == 1) {
                        that.setData({
                            choose: i + 1
                        });
                        break;
                    }
                }
            },
            fail: res => {}
        });
    }
});